﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace MotionControl.Modules.LightSource.LightControl
{
    public class LightControlManger
    {
        string Path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "LightConfig.ini";
        string ErroeInfo = null;
        string TypeLightControl = null;

        public struct LightControlInfo
        {
            public LightControlInfo(string _type, int _lightNum, string _com)
            { 
                LightType = _type;
                ChanelNums = _lightNum;
                Com = _com;
            }
            public string LightType;
            public int ChanelNums;
            public string Com;
        }
        int ControlsNum = 0;
        List<IJFLight> lstDev = null;
        Dictionary<int, LightControlInfo> dicInfo = null;
        DataOperIni dt = null;
        public LightControlManger()
        {
            dt = new DataOperIni();
        }
        public string GetErrorInfo()
        {
            return ErroeInfo;
        }
        public bool InitAllControlers()
        {
            if (File.Exists(Path))
            {
                ControlsNum = Convert.ToInt32(dt.ReadString("Total", "LightCounts", "5", Path));

                if (null == dicInfo)
                    dicInfo = new Dictionary<int, LightControlInfo>();

                dicInfo.Clear();
                for (int index = 0; index < ControlsNum; index++)
                {
                    string section = "Light" + (index + 1);
                    string key = "TYPE";
                    string def = "OPT";
                    string stype = dt.ReadString(section, key, def, Path);

                    string key2 = "ChanelNum";
                    string def2 = "2";
                    int chanelnum = Convert.ToInt32(dt.ReadString(section, key2, def2, Path));

                    string key3 = "COM";
                    string def3 = "COM" + (index + 1);
                    string com = dt.ReadString(section, key3, def3, Path);


                    LightControlInfo info = new LightControlInfo(stype, chanelnum, com);
                    dicInfo.Add(index, info);
                }
                lstDev = new List<IJFLight>();
                for (int i = 0; i < ControlsNum; i++)
                {
                    if ("OPT" == dicInfo[i].LightType)
                    {
                        JCOptLight opt = new JCOptLight(dicInfo[i].Com, dicInfo[i].ChanelNums);
                        if (!opt.InitLight())
                        {
                            ErroeInfo = opt.GetErrorInfo();
                            return false;
                        }
                        lstDev.Add(opt);
                    }
                    else
                    {
                        JCKwLight kw = new JCKwLight(dicInfo[i].Com, dicInfo[i].ChanelNums);
                        if (!kw.InitLight())
                        {
                            ErroeInfo = kw.GetErrorInfo();
                            return false;
                        }
                        lstDev.Add(kw);
                    }
                }
            }
            else
            {
                ErroeInfo = "光源配置文件不存在！";
                return false;
            }
            return true;
        }

        public bool SetControlerIntensity(int lightIndex, int chanel, int lightval)
        {

            if ((lightIndex < 0 || lightIndex > ControlsNum))
            {
                ErroeInfo = "Out Of Range";
                return false;
            }
            if (!lstDev[lightIndex].SetIntensity(chanel, lightval))
            {
                ErroeInfo = lstDev[lightIndex].GetErrorInfo();
                return false;
            }
            return true;
        }

        public bool GetControlerIntensity(int lightIndex, int chanel, out int lightval)
        {
            lightval = 0;
            if ((lightIndex < 0 || lightIndex > ControlsNum))
            {
                ErroeInfo = "Out Of Range";
                return false;
            }
            if (!lstDev[lightIndex].GetIntensity(chanel, out lightval))
            {
                ErroeInfo = lstDev[lightIndex].GetErrorInfo();
                return false;
            }
            return true;
        }

        public bool TurnOffAllLight()
        {
            //for (int indexer = 0; indexer < ControlsNum; indexer++)
            //{
            //    if (!lstDev[indexer].TurnOffChannel(indexer))
            //    {
            //        ErroeInfo = lstDev[indexer].GetErrorInfo();
            //        return false;
            //    }
            //}
            return true;
        }

        public bool GetDevChanelNum(int lightIndex, out int chanelNum)
        {
            chanelNum = 0;
            try
            {
                if (null == dicInfo)
                {
                    ErroeInfo = "获取通道数量失败";
                    return false;
                }
                if (lightIndex >= 0 && lightIndex < dicInfo.Count)
                {
                    chanelNum = dicInfo[lightIndex].ChanelNums;
                }
                else
                {
                    ErroeInfo = "获取通道数量失败";
                    return false;
                }
                return true;
            }
            catch (Exception e)
            {

                ErroeInfo = "获取通道数量失败";
                return false;
            }
        }

        public bool DisConnect()
        {
            try
            {
                for (int indexer = 0; indexer < ControlsNum; indexer++)
                {
                    if (!lstDev[indexer].DisConnect())
                    {
                        ErroeInfo = lstDev[indexer].GetErrorInfo();
                        return false;
                    }
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 控制器的数量
        /// </summary>
        /// <returns></returns>
        public int devNumber()
        {
            return lstDev.Count();
        }

        public LightControlInfo devInfo(int idx)
        {
            return new LightControlInfo(lstDev[idx].type(), lstDev[idx].channels(), lstDev[idx].comIndex());
        }
    }
}
